<?php

global $_W, $_GPC;
$weid = $this->_weid;
$action = 'start';
$title = $this->actions_titles[$action];
load()->func('tpl');
$storeid = intval($_GPC['storeid']);
$setting = $this->getSetting();

$storelist = pdo_fetchall("SELECT * FROM " . tablename($this->table_stores) . " WHERE weid = :weid", array(':weid' => $weid), 'id');
$GLOBALS['frames'] = $this->getMainMenu();

$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
if ($operation == 'display') {
    if ($storeid > 0) {
        $condition = " AND storeid={$storeid} ";
    }

    if (!empty($_GPC['time'])) {
        $starttime = strtotime($_GPC['time']['start']);
        $endtime = strtotime($_GPC['time']['end']);
        $condition .= " AND dateline >= {$starttime} AND dateline <= {$endtime} ";
    }
    if (empty($starttime) || empty($endtime)) {
        $starttime = strtotime('-1 month');
        $endtime = TIMESTAMP;
    }

    $detail_total_order_price = pdo_fetchcolumn("SELECT sum(totalprice) FROM " . tablename($this->table_order) . " WHERE
weid=:weid AND status=3 AND ispay=1 $condition ", array(':weid' => $this->_weid));

    $detail_total_order_count = pdo_fetchcolumn("SELECT COUNT(1) FROM " . tablename($this->table_order) . " WHERE weid=:weid  $condition", array(':weid' => $this->_weid));

    $detail_online_totalprice = pdo_fetchcolumn("SELECT sum(totalprice) FROM " . tablename($this->table_order) . " WHERE weid = :weid AND ispay=1 AND
status=3 AND (paytype=2 OR paytype=1 OR paytype=4) $condition ", array(':weid' => $weid));

//营业详情
    $data = pdo_fetchall('SELECT * FROM ' . tablename($this->table_order) . " WHERE weid = :weid AND status=3 AND ispay=1 $condition ORDER BY
dateline DESC LIMIT 3000", array(':weid' => $this->_weid));
    $total = array();

    $footer_totalprice = 0;
    $footer_totalcount = 0;
    $footer_totalprice1 = 0;
    $footer_totalprice2 = 0;
    $footer_totalprice3 = 0;
    $footer_totalprice4 = 0;
    if (!empty($data)) {
        foreach ($data as &$da) {
            $total_price = $da['totalprice'];
            $key = date('Y-m-d', $da['dateline']);
            $return[$key]['totalprice'] += $total_price;
            $footer_totalprice += $total_price;
            $footer_totalcount += 1;
            $return[$key]['count'] += 1;
            $total['total_price'] += $total_price;
            $total['total_count'] += 1;

            if ($da['paytype'] == '1') {
                //余额
                $return[$key]['1'] += $total_price;
                $footer_totalprice1 += $total_price;
            } elseif ($da['paytype'] == '2') {
                //微信
                $return[$key]['2'] += $total_price;
                $footer_totalprice2 += $total_price;
            } elseif ($da['paytype'] == '4') {
                //支付宝
                $return[$key]['4'] += $total_price;
                $footer_totalprice4 += $total_price;
            } elseif ($da['paytype'] == '3' ||
                    $da['paytype'] == '0') {
                $return[$key]['3'] += $total_price;
                $footer_totalprice3 += $total_price;
            }
        }
        if ($_GPC['out_put'] == 'output') {
            foreach ($return as $key => $row) {
                $arr[$i]['date'] = $key;
                $arr[$i]['totalprice'] = sprintf('%.3f', $row['totalprice']);;
                $arr[$i]['count'] = intval($row['count']) . '单';
                $arr[$i]['1'] = sprintf('%.3f', $row['1']) . '元';
                $arr[$i]['2'] = sprintf('%.3f', $row['2']) . '元';
                $arr[$i]['3'] = sprintf('%.3f', $row['4']) . '元';
                $arr[$i]['4'] = sprintf('%.3f', $row['3']) . '元';
                $i++;
            }
            $this->exportexcel($arr, array('时间', '营业额', '订单数', '余额支付', '微信支付', '支付宝', '现金支付'), TIMESTAMP);
            exit();
        }
    }
} else if ($operation == 'shop') {
    if ($_GPC['ac'] == 'checkall') { //审核
        $date = $_GPC['time'];;
        $where = " WHERE weid ={$weid} AND is_show=1 ";
        $list = pdo_fetchall("SELECT * FROM " . tablename($this->table_stores) . " {$where} order by displayorder desc,id desc ");
        foreach ($list as $key => $value) {
            $logid = $value['id'];
            $log = pdo_get('weisrc_dish_marketinghandlelog', array('weid' => $weid, 'date' => $date, 'logid' =>
                $logid, 'type' => 1));
            if (empty($log)) {
                $log = array(
                    'weid' => $_W['uniacid'],
                    'type' => 1,
                    'logid' => $logid,
                    'date' => $date,
                    'dateline' => TIMESTAMP,
                );
                pdo_insert('weisrc_dish_marketinghandlelog', $log);
            }
        }
        message('审核成功！', referer(), 'success');
    }elseif ($_GPC['ac'] == 'check') { //审核
        $date = $_GPC['date'];
        $logid = $_GPC['logid'];

        $log = pdo_get('weisrc_dish_marketinghandlelog', array('weid' => $weid, 'date' => $date, 'logid' => $logid));
        if (empty($log)) {
            $log = array(
                'weid' => $_W['uniacid'],
                'type' => 1,
                'logid' => $logid,
                'date' => $date,
                'dateline' => TIMESTAMP,
            );
            pdo_insert('weisrc_dish_marketinghandlelog', $log);
        }
        message('审核成功！', referer(), 'success');
    } elseif ($_GPC['ac'] == 'detail') { //交易详情
        $storeid = intval($_GPC['storeid']);
        $date = $_GPC['date'];

        $list = pdo_fetchall("SELECT * FROM " . tablename($this->table_order) . " WHERE weid=:weid AND storeid=:storeid  AND status=3 AND (paytype=1 OR paytype=2 OR paytype=4) AND ispay=1 AND
ismerge=0 and  date_format(FROM_UNIXTIME(dateline), '%Y-%m-%d') = :date ORDER BY id DESC", array(':weid' => $this->_weid, ':storeid' => $storeid, ':date' => $date));
    } else {
        if (!empty($_GPC['time'])) {
            $starttime = strtotime($_GPC['time']);
            $selecttime = $_GPC['time'];
        } else {
            $starttime = TIMESTAMP;
            $selecttime = date('Y-m-d');
        }

        $loglist = pdo_fetchall("SELECT * FROM " . tablename('weisrc_dish_marketinghandlelog') . " WHERE weid = :weid AND type=1 and  date = :date  ", array(':weid' => $weid, ':date' => $selecttime), 'logid');
        $where = " WHERE weid ={$weid} AND is_show=1 ";
        $list = pdo_fetchall("SELECT * FROM " . tablename($this->table_stores) . " {$where} order by displayorder desc,id desc ");
        foreach ($list as $key => $value) {
            //日交易总金额
            $totalprice = pdo_fetchcolumn("SELECT sum(totalprice) FROM " . tablename($this->table_order) . " WHERE weid=:weid AND storeid=:storeid AND status=3 AND (paytype=1 OR paytype=2 OR paytype=4) AND ispay=1 AND ismerge=0 and  date_format(FROM_UNIXTIME(dateline), '%Y-%m-%d') = :date ", array(':weid' => $this->_weid, ':storeid' => $value['id'], ':date' => $selecttime));
            $totalprice = sprintf('%.3f', $totalprice);
            $list[$key]['totalprice'] = $totalprice;
        }

//        if ($_GPC['out_put'] == 'output') {
//            foreach ($list as $key => $row) {
//                $arr[$i]['title'] = $row['title'];
//                $arr[$i]['totalprice'] = sprintf('%.3f', $row['totalprice']);
//                if ($row['is_default_rate']==1) {
//                    $show_fee_rate = intval($setting['fee_rate']);
//                } else {
//                    $show_fee_rate = intval($row['fee_rate']);
//                }
//                $arr[$i]['show_fee_rate'] = $show_fee_rate . '%';
//                $fee= floatval($row['totalprice'])*floatval($show_fee_rate)/100;
//                $arr[$i]['fee'] = $row['totalprice']-$fee . '元';
//                $arr[$i]['bank'] = "'" . $row['business_bankcard'];
//                $arr[$i]['username'] = $row['business_username'];
//                $i++;
//            }
//            $this->exportexcel($arr, array('门店名称', '日营业额', '门店费率', '提现金额', '银行卡号', '姓名'), TIMESTAMP);
//            exit();
//        }
        if ($_GPC['out_put'] == 'output') {
            $out_count = 0;
            $out_totalprice = 0;
            $out_index = 1;
            foreach ($list as $key => $row) {
                $arr[$i]['index'] = $out_index;
                $arr[$i]['out_bankname'] = $setting['pay_bankname'];
                $arr[$i]['business_bankcard'] = "'" . $row['business_bankcard'];
                $arr[$i]['username'] = $row['business_username'];
                if ($row['is_default_rate']==1) {
                    $show_fee_rate = intval($setting['fee_rate']);
                } else {
                    $show_fee_rate = intval($row['fee_rate']);
                }
                $fee= floatval($row['totalprice'])*floatval($show_fee_rate)/100;
                $arr[$i]['fee'] = $row['totalprice']-$fee;
                $out_totalprice += $row['totalprice']-$fee;
                $out_count++;
                $out_index++;
                $i++;
            }
            $title[0] = array(
                'out_count' => $out_count,
                'out_totalprice' => $out_totalprice,
                'out_coin' => 'mop',
                'in_coin' => 'mop',
            );

            $this->exportexcel2(
                $title,
                array('总笔数', '总金额', '转出币种', '转入币种'),
                $arr,
                array('顺序号', '转出账号', '转入账号', '转入户名', '转账金额'),
                TIMESTAMP);
            exit();
        }



    }
} else if ($operation == 'marketing') {
    if ($_GPC['ac'] == 'checkall'){
        $date = $_GPC['time'];;
        $where = " WHERE weid ={$weid} ";
        $list = pdo_fetchall("SELECT * FROM " . tablename('weisrc_dish_marketing') . " {$where} order by displayorder desc,id desc ");
        foreach ($list as $key => $value) {
            $logid = $value['id'];
            $log = pdo_get('weisrc_dish_marketinghandlelog', array('weid' => $weid, 'date' => $date, 'logid' =>
                $logid, 'type' => 2));
            if (empty($log)) {
                $log = array(
                    'weid' => $_W['uniacid'],
                    'type' => 2,
                    'logid' => $logid,
                    'date' => $date,
                    'dateline' => TIMESTAMP,
                );
                pdo_insert('weisrc_dish_marketinghandlelog', $log);
            }
        }
        message('审核成功！', referer(), 'success');
    } elseif ($_GPC['ac'] == 'check') { //审核
        $date = $_GPC['date'];
        $logid = $_GPC['logid'];

        $log = pdo_get('weisrc_dish_marketinghandlelog', array('weid' => $weid, 'date' => $date, 'logid' => $logid));
        if (empty($log)) {
            $log = array(
                'weid' => $_W['uniacid'],
                'type' => 2,
                'logid' => $logid,
                'date' => $date,
                'dateline' => TIMESTAMP,
            );
            pdo_insert('weisrc_dish_marketinghandlelog', $log);
        }
        message('审核成功！', referer(), 'success');
    } elseif ($_GPC['ac'] == 'detail') { //交易详情
        $marketingid = intval($_GPC['marketingid']);
        $date = $_GPC['date'];

//        $list = pdo_fetchall("SELECT * FROM " . tablename($this->table_order) . " WHERE weid=:weid AND marketingid=:marketingid  AND status=3 AND (paytype=1 OR paytype=2 OR paytype=4) AND ispay=1 AND
//ismerge=0 and  date_format(FROM_UNIXTIME(dateline), '%Y-%m-%d') = :date ORDER BY id DESC", array(':weid' => $this->_weid, ':marketingid' => $marketingid, ':date' => $date));
        $starttime = $_GPC['starttime'];
        $endtime = $_GPC['endtime'];
        $condition .= " AND dateline >= {$starttime} AND dateline <= {$endtime} ";

        $where = " WHERE weid ={$weid} AND is_show=1 AND marketingid={$marketingid} {$condition}";
        $list = pdo_fetchall("SELECT * FROM " . tablename($this->table_stores) . " {$where} order by displayorder desc,id desc ");
        foreach ($list as $key => $value) {
            $marketingprice = pdo_fetchcolumn("SELECT sum(marketingprice) FROM " . tablename($this->table_order) . " WHERE weid=:weid AND marketingid=:marketingid AND status=3 AND (paytype=1 OR paytype=2 OR paytype=4)
 AND ispay=1 AND ismerge=0 {$condition} AND storeid=:storeid", array(':weid' => $this->_weid, ':marketingid' => $marketingid, ':storeid' => $value['id']));
            $marketingprice = sprintf('%.3f', $marketingprice);
            $list[$key]['marketingprice'] = $marketingprice;

            //日交易总金额
            $totalprice = pdo_fetchcolumn("SELECT sum(totalprice) FROM " . tablename($this->table_order) . " WHERE weid=:weid AND marketingid=:marketingid  AND status=3 AND (paytype=1 OR paytype=2 OR paytype=4)
 AND ispay=1 AND ismerge=0 {$condition} AND storeid=:storeid", array(':weid' => $this->_weid, ':marketingid' => $marketingid, ':storeid' => $value['id']));

            $totalprice = sprintf('%.3f', $totalprice);
            $list[$key]['totalprice'] = $totalprice;
        }
    } else {

        $condition = '';
        if (!empty($_GPC['time'])) {
            $starttime = strtotime($_GPC['time']['start']);
            $endtime = strtotime($_GPC['time']['end']);
        }
        if (empty($starttime) || empty($endtime)) {
            $starttime = strtotime('-1 month');
            $endtime = TIMESTAMP;
        }
        $condition .= " AND dateline >= {$starttime} AND dateline <= {$endtime} ";

        $loglist = pdo_fetchall("SELECT * FROM " . tablename('weisrc_dish_marketinghandlelog') . " WHERE weid = :weid AND type=2 {$condition}", array(':weid' => $weid), 'logid');

        $levellist = pdo_fetchall("SELECT * FROM " . tablename("weisrc_dish_marketinglevel") . " WHERE weid = :weid  ORDER BY id DESC", array(':weid' => $this->_weid),'id');

        $where = " WHERE weid ={$weid} ";
        //专员
        $list = pdo_fetchall("SELECT * FROM " . tablename('weisrc_dish_marketing') . " {$where} order by displayorder desc,id desc ");
        foreach ($list as $key => $value) {
            //日交易总金额
            $totalprice = pdo_fetchcolumn("SELECT sum(totalprice) FROM " . tablename($this->table_order) . " WHERE weid=:weid AND marketingid=:marketingid  AND status=3 AND (paytype=1 OR paytype=2 OR paytype=4)
 AND ispay=1 AND ismerge=0 {$condition} ", array(':weid' => $this->_weid, ':marketingid' => $value['id']));
            $marketingprice = pdo_fetchcolumn("SELECT sum(marketingprice) FROM " . tablename($this->table_order) . " WHERE weid=:weid AND marketingid=:marketingid  AND status=3 AND (paytype=1 OR paytype=2 OR paytype=4)
 AND ispay=1 AND ismerge=0 {$condition} ", array(':weid' => $this->_weid, ':marketingid' => $value['id']));

            $marketingprice = sprintf('%.3f', $marketingprice);
            $totalprice = sprintf('%.3f', $totalprice);
            $list[$key]['totalprice'] = $totalprice;
            $list[$key]['marketingprice'] = $marketingprice;
            $list[$key]['fee_rate'] = floatval($levellist[$value['levelid']]['fee_rate']);
            //-------------------------------
            $allmarketingprice = pdo_fetchcolumn("SELECT sum(marketingprice) FROM " . tablename($this->table_order) . " WHERE weid=:weid AND marketingid=:marketingid  AND status=3 AND (paytype=1 OR paytype=2 OR paytype=4)
 AND ispay=1 AND ismerge=0 ", array(':weid' => $this->_weid, ':marketingid' => $value['id']));
            $list[$key]['allmarketingprice'] = $allmarketingprice;
        }

        if ($_GPC['out_put'] == 'output') {
            foreach ($list as $key => $value) {
                //日交易总金额
                $totalprice = pdo_fetchcolumn("SELECT sum(totalprice) FROM " . tablename($this->table_order) . " WHERE weid=:weid AND marketingid=:marketingid  AND status=3 AND (paytype=1 OR paytype=2 OR paytype=4)
 AND ispay=1 AND ismerge=0 {$condition} ", array(':weid' => $this->_weid, ':marketingid' => $value['id']));
                $marketingprice = pdo_fetchcolumn("SELECT sum(marketingprice) FROM " . tablename($this->table_order) . " WHERE weid=:weid AND marketingid=:marketingid AND status=3 AND (paytype=1 OR paytype=2 OR paytype=4)
 AND ispay=1 AND ismerge=0 {$condition} ", array(':weid' => $this->_weid, ':marketingid' => $value['id']));
                $marketingprice = sprintf('%.3f', $marketingprice);
                $totalprice = sprintf('%.3f', $totalprice);

                $arr[$i]['username'] = $value['username'];
                $arr[$i]['totalprice'] = $totalprice;
                $arr[$i]['fee_rate'] = floatval($levellist[$value['levelid']]['fee_rate']) . '%';
                $arr[$i]['marketingprice'] = $marketingprice;
                $arr[$i]['bankcard'] = $value['bankcard'];
                $arr[$i]['username'] = $value['username'];
                $i++;
            }
            $this->exportexcel($arr, array('市场专员', '营业额', '提成比例', '提成金额', '银行卡', '姓名'), TIMESTAMP);
            exit();
        }
    }
} else if ($operation == 'main') {
//    if (!empty($_GPC['time'])) {
//        $starttime = strtotime($_GPC['time']);
//        $selecttime = $_GPC['time'];
//    } else {
//        $starttime = TIMESTAMP;
//        $selecttime = date('Y-m-d');
//    }
    $condition = '';
    if (!empty($_GPC['time'])) {
        $starttime = strtotime($_GPC['time']['start']);
        $endtime = strtotime($_GPC['time']['end']);
    }
    if (empty($starttime) || empty($endtime)) {
        $starttime = strtotime('-1 month');
        $endtime = TIMESTAMP;
    }
    $condition .= " AND dateline >= {$starttime} AND dateline <= {$endtime} ";

    $where = " WHERE weid ={$weid} AND is_show=1 ";
    $list = pdo_fetchall("SELECT * FROM " . tablename($this->table_stores) . " {$where} order by displayorder desc,id desc ");
    foreach ($list as $key => $value) {
        //日交易总金额
        $totalprice = pdo_fetchcolumn("SELECT sum(totalprice) FROM " . tablename($this->table_order) . " WHERE weid=:weid AND storeid=:storeid AND status=3 AND
(paytype=1 OR paytype=2 OR paytype=4) AND ispay=1 AND ismerge=0 {$condition}", array(':weid' => $this->_weid, ':storeid' => $value['id']));
        $totalprice = sprintf('%.3f', $totalprice);
        $list[$key]['totalprice'] = $totalprice;

        if ($value['is_default_rate']==1) {
            $show_fee_rate = intval($setting['fee_rate']);
        } else {
            $show_fee_rate = intval($value['fee_rate']);
        }
        $fee_price = $totalprice*$show_fee_rate/100;
        $fee_price = sprintf('%.3f', $fee_price);
        $list[$key]['fee_price'] = $fee_price;

        $payment_price = $totalprice*intval($setting['payment_rate'])/100;
        $list[$key]['payment_price'] = $payment_price;

        $marketingprice = pdo_fetchcolumn("SELECT sum(marketingprice) FROM " . tablename($this->table_order) . " WHERE weid=:weid AND storeid=:storeid AND status=3 AND (paytype=1 OR paytype=2 OR paytype=4) AND ispay=1 AND ismerge=0  {$condition} ", array(':weid' => $this->_weid, ':storeid' => $value['id']));
        $marketingprice = sprintf('%.3f', $marketingprice);

        $list[$key]['marketingprice'] = $marketingprice;

        $list[$key]['price'] = $fee_price - $payment_price - $marketingprice;
    }




    if ($_GPC['out_put'] == 'output') {
        foreach ($list as $key => $value) {
            //日交易总金额
            $totalprice = pdo_fetchcolumn("SELECT sum(totalprice) FROM " . tablename($this->table_order) . " WHERE weid=:weid AND storeid=:storeid AND status=3 AND (paytype=1 OR paytype=2 OR paytype=4) AND ispay=1 AND ismerge=0 {$condition}", array(':weid' => $this->_weid, ':storeid' => $value['id']));
            $totalprice = sprintf('%.3f', $totalprice);
            $list[$key]['totalprice'] = $totalprice;

            if ($value['is_default_rate']==1) {
                $show_fee_rate = intval($setting['fee_rate']);
            } else {
                $show_fee_rate = intval($value['fee_rate']);
            }
            $fee_price = $totalprice*$show_fee_rate/100;
            $fee_price = sprintf('%.3f', $fee_price);
            $payment_price = $totalprice*intval($setting['payment_rate'])/100;
            $marketingprice = pdo_fetchcolumn("SELECT sum(marketingprice) FROM " . tablename($this->table_order) . " WHERE weid=:weid AND storeid=:storeid AND status=3 AND (paytype=1 OR paytype=2 OR paytype=4) AND ispay=1 AND ismerge=0 {$condition} ", array(':weid' => $this->_weid, ':storeid' => $value['id']));
            $marketingprice = sprintf('%.3f', $marketingprice);


            $arr[$i]['title'] = $value['title'];
            $arr[$i]['totalprice'] = $totalprice;
            $arr[$i]['fee_price'] = $fee_price;
            $arr[$i]['payment_price'] = $payment_price;
            $arr[$i]['marketingprice'] = $marketingprice;
            $arr[$i]['price'] = $fee_price - $payment_price - $marketingprice;;
            $i++;
        }
        $this->exportexcel($arr, array('门店名称', '营业额', '平台抽佣金额', '支付通道费', '经理提成利润', '平台利润'), TIMESTAMP);
        exit();
    }

}

include $this->template('web/financestatis');
